home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / rstrpos.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  63 lines

  1. ; $Id: rstrpos.pro,v 1.3 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1993-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. FUNCTION RSTRPOS, Expr, SubStr, Pos
  7. ;+
  8. ; NAME:
  9. ;       RSTRPOS
  10. ;
  11. ; PURPOSE:
  12. ;    This function finds the last occurrence of a substring within
  13. ;    an object string. If the substring is found in the expression,
  14. ;    RSTRPOS returns the character position of the match, otherwise
  15. ;    it returns -1.
  16. ;
  17. ; CATEGORY:
  18. ;    String processing.
  19. ;
  20. ; CALLING SEQUENCE:
  21. ;        Result = RSTRPOS(Expr, SubStr [, Pos])
  22. ;
  23. ; INPUTS:
  24. ;       Expr:    The expression string in which to search for the substring.
  25. ;    SubStr: The substring to search for.
  26. ;
  27. ; OPTIONAL INPUTS:
  28. ;    Pos:    The character position before which the search is bugun.
  29. ;              If Pos is omitted, the search begins at the last character
  30. ;              of Expr.
  31. ;
  32. ; OUTPUTS:
  33. ;        Returns the position of the substring, or -1 if the
  34. ;     substring was not found within Expr.
  35. ;
  36. ; SIDE EFFECTS:
  37. ;        Unlike STRPOS, Expr and SubStr must be strings.
  38. ;
  39. ; EXAMPLE:
  40. ;    Expr = 'Holy smokes, Batman!'    ; define the expression.
  41. ;    Where = RSTRPOS(Exp, 'smokes')    ; find position.
  42. ;    Print, Where            ; print position.
  43. ;        5            ; substring begins at position 5
  44. ;                    ; (the sixth character).
  45. ;
  46. ; MODIFICATION HISTORY:
  47. ;        JWG, January, 1993
  48. ;-
  49.     Len    = STRLEN(Expr)
  50.     IF N_ELEMENTS(Pos) EQ 0 THEN Start=0 ELSE Start = Len - Pos
  51.  
  52.     ;    Reverse the string
  53.     RString    = REVERSE(BYTE(Expr))
  54.  
  55.     ;    Reverse the substring
  56.  
  57.     RSubStr    = REVERSE(BYTE(SubStr))
  58.  
  59.     SubPos    = STRPOS(STRING(RString),STRING(RSubStr),Start)
  60.     IF SubPos NE -1 THEN SubPos = Len - SubPos - STRLEN(SubStr)
  61.     RETURN, SubPos
  62. END
  63.